package com.example.xunua_zhinengjiaotong.Dialog;


import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.BusStationBean;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class BusDialog1 extends AlertDialog {
    private int num;
    private Context context;
    private Button btn_chaxun;
    private TextView tv_title,tv1,tv2,rl_tv1,rl_tv2;  //标题，公交A距离，公交B距离， 第三行使用的rl_tv1和rl_tv2
    private Spinner spinner;
    private RelativeLayout rl;
    private LinearLayout line;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();
    private ArrayList<BusStationBean> list;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case -1:
                    Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    line.setVisibility(View.VISIBLE);
                    tv1.setText(list.get(0).getBusId()+"号公交距离"+list.get(0).getDistance()+"米");
                    tv2.setText(list.get(1).getBusId()+"号公交距离"+list.get(1).getDistance()+"米");
                    break;
                case 10:
                    rl_tv2.setText(msg.obj+"");
                    break;
            }
        }
    };

    public BusDialog1(int num, Context context,MyApp myApp) {
        super(context, R.style.myDialog);
        this.context=context;
        this.num=num;
        this.myApp=myApp;
    }

    protected BusDialog1(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected BusDialog1(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_bus1);
        initView();
        if (num == 0) {
            spinner.setAdapter(new ArrayAdapter<>(context,R.layout.support_simple_spinner_dropdown_item,new String[]{"1号站台","2号站台"}));
        }else if (num==1){
            tv_title.setText("路况");
            rl.setVisibility(View.VISIBLE);
            spinner.setAdapter(new ArrayAdapter<>(context,R.layout.support_simple_spinner_dropdown_item,new String[]{"1号公路","2号公路"
                    ,"3号公路","4号公路","5号公路","6号公路","7号公路","8号公路"}));
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    rl_tv1.setText(spinner.getSelectedItem().toString()+"路况：");
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
        btn_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num == 0) {//查询公交站台信息
                    list.clear();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                //接口有问题。 就一个请求能用{"BusStationId":1}     {"BusStationId":2}和{"BusStationId":0}试了都不行
//                                JSONArray getBusStationInfo = util.connectServer_JsonArray("{\"BusStationId\":"+spinner.getSelectedItemId()+1+"}", myApp, "GetBusStationInfo");


                                JSONArray getBusStationInfo = util.connectServer_JsonArray("{\"BusStationId\":1}", myApp, "GetBusStationInfo");
                                for (int i = 0; i < getBusStationInfo.length(); i++) {
                                    String s = getBusStationInfo.getString(i);
                                    JSONObject jsonObject = new JSONObject(s);
                                    String distance = jsonObject.getString("Distance");
                                    String busId = jsonObject.getString("BusId");
                                    BusStationBean bean=new BusStationBean(distance,busId);
                                    list.add(bean);
                                }
                                handler.sendEmptyMessage(0);
                            }catch (Exception e){
                                handler.sendEmptyMessage(-1);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
                if (num == 1) {//查询公路路况
                    final int id= (int) (spinner.getSelectedItemId()+1);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject getBusLocationAction = util.connectServer("{\"RoadId\":"+id+"}", myApp, "GetRoadStatus");
                                String s = getBusLocationAction.getString("Status");
                                Message message = new Message();
                                message.what=10;
                                message.obj=s;
                                handler.sendMessage(message);
                            }catch (Exception e){
                                handler.sendEmptyMessage(-1);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
    }

    private void initView() {
        list=new ArrayList();
        line=findViewById(R.id.bus1_line);
        rl=findViewById(R.id.bus1_rl);
        btn_chaxun=findViewById(R.id.bus1_btn_chaxun);
        tv_title=findViewById(R.id.bus1_tv_title);
        tv1=findViewById(R.id.bus1_tv1);
        tv2=findViewById(R.id.bus1_tv2);
        rl_tv1=findViewById(R.id.bus1_rl_tv1);
        rl_tv2=findViewById(R.id.bus1_rl_tv2);
        spinner=findViewById(R.id.bus1_spinner);
    }
}
